package edu.utfpr.view;

import edu.utfpr.view.R.id;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ViewComparar extends Activity {

	private static final int REQUEST_OPEN_CMP1 = 1;
	private static final int REQUEST_OPEN_CMP2 = 2;
	private ImageView imageview1;
	private ImageView imageview2;
	private Bitmap image1 = null;
	private Bitmap image2 = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CarregaComparar();
	}

	public void CarregaComparar() {

		setContentView(R.layout.comparar);

		// Referencias
		Button btfoto1 = (Button) findViewById(R.id.btfoto1);
		Button btfoto2 = (Button) findViewById(R.id.btfoto2);
		Button btcomparar = (Button) findViewById(R.id.btcomparar);
		Button btvolta = (Button) findViewById(R.id.btvolta);

		imageview1 = (ImageView) findViewById(id.imagefoto1);
		imageview2 = (ImageView) findViewById(id.imagefoto2);

		btfoto1.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent photo = new Intent(Intent.ACTION_PICK);
				photo.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				photo.setType("image/*");
				startActivityForResult(photo, REQUEST_OPEN_CMP1);
			}

		});

		btfoto2.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent photo = new Intent(Intent.ACTION_PICK);
				photo.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				photo.setType("image/*");
				startActivityForResult(photo, REQUEST_OPEN_CMP2);
			}

		});

		btcomparar.setOnClickListener(new View.OnClickListener() {
			// Evento para comparar as duas fotos a partir do histograma gerado
			// delas
			public void onClick(View v) {
				if (image1 == null && image2 == null) {
					AlertDialog.Builder dialogo = new AlertDialog.Builder(
							ViewComparar.this);
					dialogo.setTitle("Erro!");
					dialogo.setMessage("Selecione as duas Imagens");
					dialogo.setNeutralButton("Ok", null);
					dialogo.show();
				} else {
					// comparar();
				}
			}
		});

		btvolta.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				try{
					Intent principal = new Intent(v.getContext(), ViewPrincipal.class);
					principal.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(principal);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK && requestCode == REQUEST_OPEN_CMP1) {
			try {
				Uri photoUri = data.getData();
				image1 = Media.getBitmap(getContentResolver(), photoUri);
				imageview1.setImageBitmap(image1);
			} catch (Exception e) {

			}
		}

		if (resultCode == RESULT_OK && requestCode == REQUEST_OPEN_CMP2) {
			try {
				Uri photoUri = data.getData();
				image2 = Media.getBitmap(getContentResolver(), photoUri);
				imageview2.setImageBitmap(image2);
			} catch (Exception e) {

			}
		}

	}
}
